<?php

/**
 * This is the model class for table "tbtt_news".
 *
 * The followings are the available columns in table 'tbtt_news':
 * @property integer $news_id
 * @property string $news_title
 * @property string $news_description
 * @property string $news_content
 * @property string $news_created_at
 * @property string $news_updated_at
 * @property integer $source_id
 * @property integer $category_id
 * @property integer $news_number_viewer
 */
class News extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return News the static model class
	 */
	
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'tbtt_news';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('news_title, news_description, news_content, news_created_at, news_updated_at, source_id, category_id', 'required'),
			array('source_id, category_id, news_number_viewer', 'numerical', 'integerOnly'=>true),
                        array('news_created_at, news_updated_at', 'safe'),
			array('news_title', 'length', 'max'=>360),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('news_id, news_title, news_description, news_content, source_id, category_id, news_number_viewer', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
                    'category' => array(self::BELONGS_TO, 'Category', 'category_id'),
                    'comments' => array(self::HAS_MANY, 'Comment', 'news_id'),
                );
	}
         public function addComment($comment)
	{
		if(Yii::app()->params['commentNeedApproval'])
			$comment->comment_status=Comment::STATUS_PENDING;
		else
			$comment->comment_status=Comment::STATUS_APPROVED;
		$comment->news_id=$this->comment_id;
		return $comment->save();
	}
	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'news_id' => 'News',
			'news_title' => 'News Title',
			'news_description' => 'News Description',
			'news_content' => 'News Content',
			'news_created_at' => 'News Created At',
			'news_updated_at' => 'News Updated At',
			'source_id' => 'Source',
			'category_id' => 'Category',
			'category' => 'Category',
			'news_number_viewer' => 'News Number Viewer',
		);
	}
	
	public function getCategoryOptions( ){
            $category = Category::model()->findAll();
            $cat = CHtml::listData($category,'category_id','category_name');
            return $cat;
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('news_id',$this->news_id);
		$criteria->compare('news_title',$this->news_title,true);
		$criteria->compare('news_description',$this->news_description,true);
		$criteria->compare('news_content',$this->news_content,true);
		$criteria->compare('news_created_at',$this->news_created_at,true);
		$criteria->compare('news_updated_at',$this->news_updated_at,true);
		$criteria->compare('source_id',$this->source_id);
		$criteria->compare('category_id',$this->category_id);
		$criteria->compare('news_number_viewer',$this->news_number_viewer);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
}